package com.ego.service.impl;

import com.ego.commons.utils.JsonUtils;
import com.ego.dubbo.service.*;
import com.ego.pojo.*;
import com.ego.service.ItemCatService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Service
@CacheConfig(cacheNames = "com.ego.item")
public class ItemCatServiceImpl implements ItemCatService {
    @DubboReference
    private TbItemCatDubboService tbItemCatDubboService;
    @DubboReference
    private TbItemDubboService itemDubboService;

    @Override
    @Cacheable(key = "'showItemCat'")
    public ItemCategoryNav showItemCat() {
        ItemCategoryNav itemCategoryNav = new ItemCategoryNav();
        itemCategoryNav.setData(getAllItemCat(0L));
        return itemCategoryNav;
    }

    @Override
    @Cacheable(key = "'itemDetail::'+#id")
    public TbItemDetails selectById(Long id) {
        TbItem tbItem =itemDubboService.selectById(id);

        TbItemDetails tbItemDetails = new TbItemDetails();
        //将TbItem对象数据copy到TbItemDetail
        BeanUtils.copyProperties(tbItem,tbItemDetails);

        tbItemDetails.setImages(StringUtils.isNoneBlank(tbItem.getImage())?tbItem.getImage().split(","):new String[1]);
        return tbItemDetails;
    }
@DubboReference
    private TbItemDescDubboService itemDescDubboService;
    @Override
    public String selectItemDescByItemId(Long id) {
        TbItemDesc tbItemDesc = itemDescDubboService.selectByItemId(id);

        return tbItemDesc.getItemDesc();
    }
@DubboReference
    private TbItemParamItemDubboService itemParamItemDubboService;
    @Override
    public String selectItemParamByItemId(Long id) {
        TbItemParamItem tbItemParamItem = itemParamItemDubboService.selectByItemId(id);
        String jsonStr = tbItemParamItem.getParamData();
        //[{"group":"主体","params":[{"k":"品牌","v":""},{"k":"型号","v":""},{"k":"颜色","v":""},{"k":"上市年份","v":""}]},{"group":"网络","params":[{"k":"4G网络制式","v":""},{"k":"3G网络制式","v":""},{"k":"2G网络制式","v":""}]},{"group":"存储","params":[{"k":"机身内存","v":""},{"k":"储存卡类型","v":""}]}]
        List<ParamData> paramData = JsonUtils.jsonToList(jsonStr, ParamData.class);
        StringBuilder sb = new StringBuilder();
        for (ParamData pd : paramData) {
            System.out.println("paramDatum = " + pd);
            sb.append("<table  style='width:100%'>");

            for (int i = 0; i < pd.getParams().size(); i++) {
                ParamDataKAndV param = pd.getParams().get(i);
                sb.append("<tr>");
                //如果是第一行显示组名,其他行就空起来
                if (i==0){
                    sb.append("<td style='width:100px;text-align:right;padding-right:30px'>"+pd.getGroup()+"</td>");
                }else{
                    sb.append("<td></td>");
                }
                sb.append("<td style='width:100px;text-align:right;padding-right:30px'>"+param.getK()+"</td>");
                sb.append("<td>"+param.getV()+"</td>");

                sb.append("</tr>");
            }

            sb.append("</table>");

            sb.append("<hr style='color:gray'>");
        }

        return sb.toString();
    }

    /**
     * 根据商品类目父id查询出对应的子类目数据
     * @param parentId 父id
     * @return 类目集合
     */
    private List<Object> getAllItemCat(long parentId){
        //消费Dubbo提供者的根据父id查询主对应的子类目的方法
        List<TbItemCat> tbItemCats = tbItemCatDubboService.selectItemCatsByParentId(parentId);
        List<Object> categoryNodes = new ArrayList<>();
            //循环集合，将每一个对象的数据重新获取出来封装成一个个                            CategoryNode对象中去
        for (TbItemCat tbItemCat : tbItemCats) {
            if (tbItemCat.getIsParent()){
                CategoryNode categoryNode = new CategoryNode();
                categoryNode.setU("/products/"+tbItemCat.getId()+".html");
                categoryNode.setN("<a href='/products/"+tbItemCat.getId()+".html'>"+tbItemCat.getName()+"</a>");
                categoryNode.setI(getAllItemCat(tbItemCat.getId()));
                //将封装CategoryNode 添加到集合中
                categoryNodes.add(categoryNode);
            }else {
                categoryNodes.add("/products/"+tbItemCat.getId()+".html|"+tbItemCat.getName());
            }
        }

        return categoryNodes;
    }
}
